package com.uperform.coach.tdd.domain;

import com.uperform.coach.tdd.SystemDate;
import lombok.Data;

@Data
public class Task {
    private int id;
    private String name;
    private long shouldExecuteAt;
    private CycleTime cycleTime;

    public Task(String name, long shouldExecuteAt, CycleTime cycleTime) {
        this.name = name;
        this.shouldExecuteAt = shouldExecuteAt;
        this.cycleTime = cycleTime;
    }

    public Task ensureCycleTimeValid() {
        this.cycleTime.ensureValid();
        return this;
    }

    public String getCycleTimeString() {
        return this.cycleTime.getTime();
    }

    public Task updateNextExecute() {
        while (this.shouldExecuteAt < SystemDate.now()) {
            this.shouldExecuteAt = this.cycleTime.next(this.shouldExecuteAt);
        }
        return this;
    }
}
